#include <ciallo_imgui/app.h>
#include <ciallo_imgui/demo_window.h>
#include <ciallo_imgui/image.h>
#include <ciallo_imgui/scope.h>
#include <iostream>
#include <thread>
class SandboxApp : public ciallo::AppBase<SandboxApp> {
  public:
    SandboxApp() = default;
    ~SandboxApp() = default;

  private:
    bool RadioButton(int handle, const char *label, int *value, int v) {
        if (v == *value)
            ImGui::PushStyleColor(
                ImGuiCol_Button,
                ImGui::GetStyle().Colors[ImGuiCol_ButtonActive]);
        bool ret = ImGui::Button(label);
        if (v == *value) {
            ImGui::PopStyleColor();
            return false;
        } else {
            if (ret)
                *value = v;
            return ret;
        }
    }

    ciallo::Texture texture;

    bool base_init() {
        texture = std::move(ciallo::Texture(1920, 1080, ciallo::Format::RGBA));

        auto thread = std::thread([&]() {
            std::vector<unsigned char> data(30 * 1920 * 1080 * 4);
            for (size_t i = 0; i < data.size(); i++) {
                data[i] = rand() % 255;
            }
            for (size_t i = 0; true; i++) {
                if (i >= 30) {
                    i = 0;
                }
                ciallo::CPUTimeScope_us() << [&]() {
                    texture.update(data.data() + i * 1920 * 1080 * 4, 1920,
                                   1080);
                };
            }
        });
        thread.detach();
        return true;
    }

    void base_run() {

        ciallo::show_background_window(texture);

        // ImGui::ShowDemoWindow();
        ciallo::WindowScope("hello") << [this]() {
            // ImGui::ShowDemoWindow();

            // ImGui::Image()
            // ImGui::ShowUserGuide();
            ImGui::Text("Hello, world! %d", selected_mode);
            // ImGui
            if (RadioButton(0, "Mode 0", &selected_mode, 0))
                ImGui::Text("Mode 0");
            if (RadioButton(1, "Mode 1", &selected_mode, 1))
                ImGui::Text("Mode 1");
            if (RadioButton(2, "Mode 2", &selected_mode, 2))
                ImGui::Text("Mode 2");
        };
    }
    // void base_cleanup() {}

    int selected_mode = -1;
    friend class ciallo::AppBase<SandboxApp>;

  public:
};

int main(int argc, char **argv) {

    SandboxApp app;
    if (app.init()) {
        app.run();
        app.cleanup();
    }
}